public with Sharing class SearchClaimsVF
{
    public String opportunityName {get; set;}
    public String claimStatus {get; set;}
    public String claimName {get; set;}
    public Date filingDate {get; set;}
    public Claim__c c {get; set;}
    
    public ID withdrawnClaimId {get; set;}
    public CLaim__c[] claimsList {get; set;}
    
    public SearchClaimsVF()
    {
        c = new Claim__c();
        claimsLIst = null;
    }
    
    public List<SelectOption> getClaimStatusList()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('select', 'Select'));
        options.add(new SelectOption('Pending Approval', 'Pending Approval'));
        options.add(new SelectOption('Approved', 'Approved'));
        options.add(new SelectOption('Rejected', 'Rejected'));
        options.add(new SelectOption('Withdrawn', 'Withdrawn'));
        return options;
    }
    
    public PageReference searchClaims()
    {
        if(claimStatus == 'select')
            claimStatus = '%';     
            
        if(claimName == '')
            claimName = '%';
        else
            claimName = '%'+claimName+'%';
        
        try
        {
	        if(c.Opportunity__c == null)
	            claimsList = [select Id, Name, Claim__c.Opportunity__c, Claim__c.Opportunity__r.Name, Claim_Status__c, Commission_Rate__c, Commission_Amount__c, Credit__c, Adjusted_Commission__c, Adjusted_Credit__c, Filing_Date__c from Claim__c where claim_status__c like :claimStatus and Name like :claimName and Filing_Date__c <= :c.Filing_Date__c order by Filing_Date__c desc limit 50];
	        else
	            claimsList = [select Id, Name, Claim__c.Opportunity__c, Claim__c.Opportunity__r.Name, Claim_Status__c, Commission_Rate__c, Commission_Amount__c, Credit__c, Adjusted_Commission__c, Adjusted_Credit__c, Filing_Date__c from Claim__c where claim_status__c like :claimStatus and Name like :claimName and Opportunity__c = :c.Opportunity__c  and Filing_Date__c <= :c.Filing_Date__c order by Filing_Date__c desc limit 50];
	        
	        if(claimsList != null && claimsList.size() == 0)
	            claimsList = null;
        }
        catch(Exception e)
        {
        	Apexpages.addMessage(new Apexpages.Message(ApexPages.severity.info ,e.getMessage()));
        }
        return null;
    }
    
    public PageReference withdrawClaim()
    {
    	Claim__c[] claimList = [select claim_status__c from Claim__c where id = :withdrawnClaimId];
    	if(claimList.size() > 0)
    	{
    		claimList[0].Claim_Status__c = 'Withdrawn';
    		try
    		{
    			update claimList;
    			searchClaims();
    		}
    		catch(Exception e)
    		{
	        	Apexpages.addMessage(new Apexpages.Message(ApexPages.severity.info ,e.getMessage()));
    		}
    	}
    	return null;
    }
}